package com.baidu.inf.iis.bcs.handler;

import com.baidu.inf.iis.bcs.http.BCSHttpResponse;
import com.baidu.inf.iis.bcs.model.ObjectListing;
import com.baidu.inf.iis.bcs.model.ObjectSummary;
import com.baidu.inf.iis.bcs.response.BaiduBCSResponse;
import flexjson.JSONDeserializer;
import java.util.*;

// Referenced classes of package com.baidu.inf.iis.bcs.handler:
//            HttpResponseHandler

public class ObjectListResponseHandler extends HttpResponseHandler {

	public ObjectListResponseHandler() {
	}

	public BaiduBCSResponse handle(BCSHttpResponse bcshttpresponse) {
		String s = getResponseContentByStr(bcshttpresponse);
		//System.out.println(s);
		JSONDeserializer jsondeserializer = new JSONDeserializer();
		HashMap hashmap = (HashMap) jsondeserializer.deserialize(s);
		ObjectListing objectlisting = new ObjectListing();
		
		if (hashmap.get("object_total") instanceof String)
			objectlisting.setObjectTotal(Integer.valueOf(
					(String) hashmap.get("object_total")).intValue());
		else
			objectlisting.setObjectTotal(((Integer) hashmap.get("object_total")).intValue());
		
		if (hashmap.get("start") instanceof String)
			objectlisting.setStart(Integer.valueOf(
					(String) hashmap.get("start")).intValue());
		else
			objectlisting.setStart(((Integer) hashmap.get("start")).intValue());
		
		if (hashmap.get("limit") instanceof String)
			objectlisting.setLimit(Integer.valueOf(
					(String) hashmap.get("limit")).intValue());
		else
			objectlisting.setLimit(((Integer) hashmap.get("limit")).intValue());
		
		objectlisting.setBucket((String) hashmap.get("bucket"));
		if (hashmap.get("prefix") != null)
			objectlisting.setPrefix((String) hashmap.get("prefix"));
		ArrayList arraylist = (ArrayList) hashmap.get("object_list");
		ObjectSummary objectsummary;
		for (Iterator iterator = arraylist.iterator(); iterator.hasNext(); objectlisting
				.addObjectSummary(objectsummary)) {
			HashMap hashmap1 = (HashMap) iterator.next();
			objectsummary = new ObjectSummary();
			objectsummary.setName((String) hashmap1.get("object"));
			objectsummary.setVersionKey((String) hashmap1.get("version_key"));
			objectsummary.setIsDir(((String) hashmap1.get("is_dir"))
					.equals("1"));
			objectsummary.setSize(Long.valueOf((String) hashmap1.get("size")));
			objectsummary.setLastModifiedTime(Long.valueOf((String) hashmap1
					.get("mdatetime")));
			objectsummary.setParentDir((String) hashmap1.get("parent_dir"));
		}

		BaiduBCSResponse baidubcsresponse = parseResponseMetadata(bcshttpresponse);
		baidubcsresponse.setResult(objectlisting);
		return baidubcsresponse;
	}
}
